package chatController;


import java.net.InetAddress;

import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;

import chatGUI.*;
import chatModel.ChatModel;
import chatModel.Conversation;
import chatModel.User;
import chatModel.*;
import chatNI.ChatNI;
/* 
 * @author Yahya-Albaba
*/

public class ChatController {
	
	/**
	 * @param  cm The chat model instance that this chat controller manipulates 
	 * @param  gui the graphic user interface that the client see and use it to send and receive messages
	 * @param  ni the chat network interface is what allows the sending and the receiving of different messages 
	*/
	
	
    private ChatModel cm;
    private ChatGui gui;
    private ChatNI ni;

    /**
     * The chat controller is a part of the model view controller design pattern that allows
     * use to organize the chat system 
     */   
    public ChatController(ChatModel cm, ChatGui gui, ChatNI ni) {
        this.cm = cm;
        this.gui = gui;
        this.ni = ni;
    }

    
    /**
     * This method is called when the client wants to get connected to the chat system
     * in this method we set the name of the the local user for this instance of the chat system
     * we start the listening thread and we send a hello with a request for a hello ack 
     */   
    public void performConnect(String username) {
        try {
            User localUser = new User(username, InetAddress.getLocalHost().getHostName());
            cm.setLocalUser(localUser);
            cm.setLocalUserIsConnected(true);
            ni.startThread();
            ni.sendHello(username, true);
        } catch (UnknownHostException ex) {
            Logger.getLogger(ChatController.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    
    
    /**
     * This method allows us to disconnect from the chat system and allowing  other users to know that
     * we are no longer connected and that they should remove the local user from their users list
     */   
    public void performDisconnect() {
        cm.setLocalUserIsConnected(false);
        ni.sendBye();
    }

    
    
    /**
     * This method allows us to send a message to a specific user, we get the user from the hashmap 
     * and then we send it using the netowrk interface
     */   
    public void performSendMessage(String username, String message) {
        ni.sendMsg(username, message);
        Conversation c=cm.createConversation(cm.getHashmapUsers().get(username));
        cm.addConversations(c);
        
    }
    
    
    /**
     * This method allows us to add a tab in the graphic user interface when the local user selects a 
     * remote user that is in the list of the connected users
     */
    public void performUserSelected(/*User remoteUser*/String username) {
    	User remoteUser=cm.getHashmapUsers().get(username);
    	System.out.println("performUserSelected"); 
    	System.out.println(remoteUser.getUsername());
    	Conversation c = new Conversation(remoteUser);
        cm.addConversations(c);
        System.out.println("on dois rajouter une tab pour l'utilisateur qu'on a choisi"); 
        gui.addTab(c);
    }

    /**
     * This method allows us to add the remote user that sent us a hello; this hello can be of a user 
     * that just got connected or from a user that was already connected  
     */
    public void performHelloRcvd(String username, String hostname) {
    	System.out.println("---------------------------------on recois un hello");
        User newUser = new User(username, hostname);
        cm.addUserInList(newUser);
    }
    
    /**
     * This method is called when we receive a message , we check if this is the beginning of a conversation
     * if this is the case then we add a tab in the graphical interface and we add the message
     * if the message have been already started we content with delivering the message  
     */
    public void performMsgRcvd(User remoteUser, String msg) {
        Conversation c = cm.getHashmapConv().get(remoteUser);//new Conversation(new User(username,hostname));
        String username = remoteUser.getUsername();
        if (c != null) //cs.getModel().getConversations().addElement(c);
        {
     //       c.addMessage(username + " :  " + msg);
            cm.addConversations(c);
        } else {
            c = cm.createConversation(remoteUser);
          //si on recois un message et on avais pas rajouter l'utilisateur
            gui.addTab(cm.getHashmapConv().get(remoteUser));
            cm.addConversations(c);
        }
        gui.getChatwindow().addMessagercvd(msg, remoteUser.getUsername());

    }

    /**
     * When a Bye is received, we remove the remote user from which we received the bye from the list
     * we then remove him from the list of the users
     */ 
    public void performByeRcvd(User remoteUser) {
        cm.removeUserFromList(remoteUser);
        String username = remoteUser.getUsername();
        gui.getChatwindow().removeUser(remoteUser.getUsername()) ;
        Conversation c = cm.getHashmapConv().get(remoteUser);
        if (c != null) {
            cm.addConversations(c);
        }
    }
    
    /**
     * When we send the bye, we close the chat window
     */ 

    public void performByeSent() {
        gui.close();
    }



}

